/*
 * @Author: Zhouhai 497675647@qq.com
 * @Date: 2022-10-08 11:47:00
 * @LastEditors: Zhouhai 497675647@qq.com
 * @LastEditTime: 2022-10-08 12:01:33
 */
import { CustomFormConfig, TTreeSelect } from "@/type.d/formItems";
import { ProFormTreeSelect } from "@ant-design/pro-components";

const getTreeEnumValueByKey: (enumtype?: string, enumValue?: Record<string, Record<string, any>[]>) => any[] = (enumtype, enumValue) => {
    if (enumtype && enumValue && enumValue[enumtype]) {
      const value = enumValue[enumtype]
      let re = [{ dictLabel: '主目录', dictValue: 0, children: value }];
      return buildTree(re);
    } else {
  
      return []
    }
  }
  
  
  const buildTree: (value: any[]) => any[] = (value) => {
  
    return value.map((item: any) => {
      return {
        title: item.dictLabel,
        value: item.dictValue,
        children: item.children ? buildTree(item.children) : []
      }
    })
  }
  
  
  
  export const RenderTreeSelect: (itemconfig: CustomFormConfig, disabled: boolean, enumValue?: Record<string, Record<string, any>[]>) => JSX.Element = (itemconfig, disabled, enumValue) => {
    const placeholder = disabled ? "" : (itemconfig?.placeholder || `请输入${itemconfig.title}`);
    const config = itemconfig.config as TTreeSelect
    return <ProFormTreeSelect
      placeholder={placeholder}
      key={itemconfig.fieldId}
      fieldProps={{ treeData: getTreeEnumValueByKey(config?.enumType, enumValue),treeDefaultExpandedKeys:[0] }}
      width="md"
      colProps={{ md: 8, xl: 6 }}
      name={itemconfig.fieldId}
      label={itemconfig.title} params={undefined} debounceTime={undefined} valueEnum={undefined} />
  }